package org.seedltd.upms.service.impl.detection;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

import org.seedltd.core.base.BasePage;
import org.seedltd.core.base.BaseServiceImpl;
import org.seedltd.core.utils.StringUtils;
import org.seedltd.upms.constant.FolderBucket;
import org.seedltd.upms.enums.EnableStatus;
import org.seedltd.upms.mapper.detection.DetectionProductMapper;
import org.seedltd.upms.model.detection.entity.DetectionProduct;
import org.seedltd.upms.model.detection.form.DetectionProductForm;
import org.seedltd.upms.model.detection.form.DetectionProductSearchForm;
import org.seedltd.upms.model.detection.vo.DetectionProductVO;
import org.seedltd.upms.service.detection.DetectionProductService;
import org.seedltd.upms.service.sys.SysAttachmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import lombok.AllArgsConstructor;

/**
 * 产品管理业务实现
 * 
 * @author laizuan
 * @since 2021/07/28
 */
@Service
@AllArgsConstructor
public class DetectionProductServiceImpl extends BaseServiceImpl<DetectionProductMapper, DetectionProduct>
    implements DetectionProductService {

    private final SysAttachmentService sysAttachmentService;

    @Override
    public DetectionProductVO findById(Long id) {
        return q().selectById(id, DetectionProductVO.class);
    }

    @Override
    public DetectionProduct getById(Serializable id) {
        return baseMapper.selectById(id);
    }

    @Override
    public Set<Long> selectIdByProductName(String productNames) {
        return baseMapper.selectIdByProductName(productNames);
    }

    @Override
    public String getDetailByProductId(Long productId) {
        return baseMapper.getDetailByProductId(productId);
    }

    @Override
    public List<DetectionProduct> listByIds(Set<Long> ids) {
        return q().in(DetectionProduct::getId, ids).eq(DetectionProduct::getEnabled, EnableStatus.ENABLE.getValue())
            .selectList();
    }

    @Override
    @Transactional
    public Long saveOrUpdate(DetectionProductForm form, MultipartFile file) throws IOException {
        boolean isUpdate = form.getId() != null;
        Long linkedId;
        if (isUpdate) {
            linkedId = this.update(form);
            if (StringUtils.isNoneEmpty(form.getDetail())) {
                baseMapper.updateDetailByProductId(linkedId, form.getDetail());
            }
        } else {
            linkedId = this.add(form);
            baseMapper.addDetailByProductId(linkedId, form.getDetail());
        }
        Long attachId = sysAttachmentService.add(file, linkedId, FolderBucket.PRODUCT.getBucketName(), null);
        if (attachId != null) {
            DetectionProduct detectionProduct = new DetectionProduct();
            detectionProduct.setId(linkedId);
            detectionProduct.setImg(attachId.toString());
            baseMapper.updateById(detectionProduct);
        }
        return linkedId;
    }

    private Long add(DetectionProductForm form) {
        return u().save(form, new DetectionProduct()).getId();
    }

    private Long update(DetectionProductForm form) {
        u().updateById(form, form.getId());
        return form.getId();
    }

    @Override
    @Transactional
    public boolean delete(Long id) {
        baseMapper.removeDetailByProductId(id);
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public List<DetectionProductVO> listValid(Integer advertising) {
        return q()
            .select(DetectionProduct::getImg, DetectionProduct::getId, DetectionProduct::getProductName,
                DetectionProduct::getProduceDesc, DetectionProduct::getPrice, DetectionProduct::getOrigPrice,
                DetectionProduct::getNecessary)
            .orderByDesc(DetectionProduct::getSortNo).orderByDesc(DetectionProduct::getCreateTime)
            .eq(advertising != null, DetectionProduct::getAdvertising, advertising)
            .selectList(DetectionProductVO.class);
    }

    @Override
    public BasePage<DetectionProductVO> page(DetectionProductSearchForm searchForm) {
        return q(searchForm).orderByDesc(DetectionProduct::getSortNo).orderByDesc(DetectionProduct::getCreateTime)
            .selectPage(searchForm.getStart(), searchForm.getSize(), DetectionProductVO.class);
    }
}